package com.aso114.project.mvp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.base.BaseFragment;
import com.aso114.project.base.BaseSimpleActivity;
import com.aso114.project.bean.QuestionBean;
import com.aso114.project.mvp.activity.fragment.ErrorExerciseLeftFragment;
import com.aso114.project.mvp.activity.fragment.ErrorExerciseRightFragment;
import com.aso114.project.util.SystemBarHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 错题练习
 */
public class EeeorExerciseActivity extends BaseSimpleActivity {

    QuestionBean qdata;
    @BindView(R.id.layout_title_bar_back_iv)
    ImageView layoutTitleBarBackIv;
    @BindView(R.id.layout_title_bar_title_tv)
    TextView layoutTitleBarTitleTv;
    @BindView(R.id.layout_title_bar_right_iv)
    ImageView layoutTitleBarRightIv;
    @BindView(R.id.layout_title_bar_right_tv)
    TextView layoutTitleBarRightTv;
    @BindView(R.id.my_collection_left)
    TextView myCollectionLeft;
    @BindView(R.id.my_collection_right)
    TextView myCollectionRight;
    @BindView(R.id.collect_fragment)
    FrameLayout collectFragment;
    @BindView(R.id.title_top_ly)
    RelativeLayout titleTopLy;

    private boolean isLeft = true;

    ErrorExerciseLeftFragment leftFragment;
    ErrorExerciseRightFragment rightFragment;

    @Override
    protected int getResouseId() {
        return R.layout.mycollect;
    }

    @Override
    protected void initView() {
        SystemBarHelper.immersiveStatusBar(this);
        SystemBarHelper.setHeightAndPadding(this, titleTopLy);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            SystemBarHelper.immersiveStatusBar(this, 0);
            SystemBarHelper.setStatusBarDarkMode(this);
        }
        layoutTitleBarTitleTv.setText("错题练习");
        setView();
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void getBroadcast(Context context, Intent intent) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.layout_title_bar_back_iv, R.id.my_collection_left, R.id.my_collection_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_title_bar_back_iv:
                finish();
                break;
            case R.id.my_collection_left:
                if (isLeft)
                    return;
                isLeft = true;
                setView();
                break;
            case R.id.my_collection_right:
                if (!isLeft) {
                    return;
                }
                isLeft = false;
                setView();
                break;
        }
    }

    public void setView() {
        myCollectionLeft.setTextColor(getResources().getColor(R.color.a333333));
        myCollectionRight.setTextColor(getResources().getColor(R.color.a333333));
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (isLeft) {
            if (leftFragment == null) {
                leftFragment = new ErrorExerciseLeftFragment();
                transaction.add(R.id.collect_fragment, leftFragment);
            }
            myCollectionLeft.setTextColor(getResources().getColor(R.color.aff6559));
            initFragment(transaction, leftFragment);
        } else {
            if (rightFragment == null) {
                rightFragment = new ErrorExerciseRightFragment();
                transaction.add(R.id.collect_fragment, rightFragment);
            }
            myCollectionRight.setTextColor(getResources().getColor(R.color.aff6559));
            initFragment(transaction, rightFragment);
        }
    }

    public void initFragment(FragmentTransaction transaction, BaseFragment currentFragment) {
        //隐藏所有fragment
        hideFragment(transaction);
        //显示需要显示的fragment
        transaction.show(currentFragment);
        //提交事务
        transaction.commit();
    }

    public void hideFragment(FragmentTransaction transaction) {
        if (leftFragment != null) {
            transaction.hide(leftFragment);
        }
        if (rightFragment != null) {
            transaction.hide(rightFragment);
        }
    }


}
